package com.yanq.liancar.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SmallCEditActivity extends BaseToolbarActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.iv_right2)
    ImageView ivRight2;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.common_toolbar)
    Toolbar commonToolbar;
    @BindView(R.id.action_bar)
    AppBarLayout actionBar;
    @BindView(R.id.et_content)
    EditText etContent;

    private String sign,username,nickname,sex,where,token;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setStatusBar(this, true,false);
        setContentView(R.layout.activity_edit);
        ButterKnife.bind(this);

        mTvTitle.setText("编辑");

        mTvRight.setVisibility(View.VISIBLE);
        mTvRight.setText("完成");
        mTvRight.setTextColor(getResources().getColor(R.color.main_color));
        where=getIntent().getExtras().getString("where");
        LogUtils.e("asdasdada",where+"");
        if ("MineFragment".equals(where)){
            token=UserBean.getcToken(this);
        }else {
            token=UserBean.getbToken(this);
        }

        mTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                // 隐藏软键盘
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                if ("MineFragment".equals(where)||"BMineFragment".equals(where)){
                    if ("Nickname".equals(getIntent().getStringExtra("Nickname"))){
                        nickname=etContent.getText().toString().trim();
                    }else if ("username".equals(getIntent().getStringExtra("username"))){
                        username=etContent.getText().toString().trim();
                    }else if ("sex".equals(getIntent().getStringExtra("sex"))){
                        sex=etContent.getText().toString().trim();
                    }else {
                        sign=etContent.getText().toString().trim();
                    }
                    initUploadMessage();
               }else {
                    Intent intent=new Intent();
                    intent.putExtra("content",etContent.getText().toString().trim());
                    setResult(RESULT_OK,intent);
                    finish();
                }
            }
        });
    }

    private void initUploadMessage() {
            OkGo.<String>post(Config.s + "llc/updateUserBaseMessage")
                .tag(this)
                .params("token", token)
                .params("sign", sign)
                .params("username", username)
                .params("nickname", nickname)
                .params("sex", sex)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        BaseBean bean = gson.fromJson(response.body(), BaseBean.class);
                        if (bean.isIsSuccess()) {
                            RxToast.success(bean.getOutputMessage());
                            finish();
                        }else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }
                });
    }
}
